﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TA.DotNetUtils
{
	public class ConvertEx
	{
		public static T To<T>(object value)
		{
			if (value is T) return (T)value;

			switch (typeof(T).FullName)
			{
				case "System.String":
					return (T)(object)value.ToString();
				case "System.Guid":
					if (value is string)
						return (T)(object)new Guid((string)value);
					else if (value is byte[])
						return (T)(object)new Guid((byte[])value);
					else
						return default(T);
				case "System.Int32":
					if (value is string)
					{
						return (T)(object)int.Parse((string)value);
					}
					break;
			}

			return (T)Convert.ChangeType(value, typeof(T));
		}
	}
}
